package com.bestv.search.management.command;

import com.bestv.search.common.dataSource.DataSourceKey;
import com.bestv.search.common.dataSource.DataSourceKeyUtil;
import com.bestv.search.common.service.InfoManager;
import com.bestv.search.common.service.TopKeywordManager;
import org.apache.log4j.Logger;

import java.util.List;

/**
 * @author Michael Wang
 */

// $Id$
public class FilterCommand {

    Logger logger = Logger.getLogger(FilterCommand.class);
    private InfoManager infoManager;
    private TopKeywordManager topKeywordManager;

    public InfoManager getInfoManager() {
        return infoManager;
    }

    public void setInfoManager(InfoManager infoManager) {
        this.infoManager = infoManager;
    }

    public TopKeywordManager getTopKeywordManager() {
        return topKeywordManager;
    }

    public void setTopKeywordManager(TopKeywordManager topKeywordManager) {
        this.topKeywordManager = topKeywordManager;
    }

    public void filter(String[] filterArr, String siteCode) {
        DataSourceKey key = DataSourceKeyUtil.convert(siteCode);
        for (int i = 0; i < filterArr.length; i++) {
            String filterWord = filterArr[i];
            logger.debug("Filtering keyword " + filterWord);
            List<String> filterInfoCodes = infoManager.getInfoCodesByNameGenreTag(key, filterWord);
            if (filterInfoCodes != null && filterInfoCodes.size() > 0) {
                for (String code : filterInfoCodes) {
                    logger.info("Filter the info with code = " + code);
                }
                // Offline filter Infos by filterInfoCodes list
                infoManager.saveOffLineInfoByCode(key, filterInfoCodes);
            }
        }
    }

    public void sensitive(String[] sensitiveArr, String siteCode) {
        DataSourceKey key = DataSourceKeyUtil.convert(siteCode);
        for (int i = 0; i < sensitiveArr.length; i++) {
            String sensitiveWord = sensitiveArr[i];
            logger.debug("Removing sensitive keyword " + sensitiveWord);
            topKeywordManager.removeTopKeyword(key, sensitiveWord);
        }
    }
}
